package com.zlx.sharelive.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.AgreementBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.Constants;
import com.zlx.sharelive.R;
import com.zlx.sharelive.utils.EncodingUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 推广
 */
public class ExtensionAc extends BaseAc {

    @BindView(R.id.tv_spread_code)
    TextView tv_spread_code;
    @BindView(R.id.iv_spread_code)
    ImageView iv_spread_code;
    @BindView(R.id.tv_rule)
    TextView tv_rule;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_extension;
    }

    @Override
    public void initViews() {
        super.initViews();

        String code = (String) SPUtil.get(this, Constants.SP_INVITE_CODE, "");
        tv_spread_code.setText("邀请码:" + code);

        Bitmap bitmap = EncodingUtils.createQRCodeBitmap("12346", 150, 150);

        iv_spread_code.setImageBitmap(bitmap);

        getAgreement("7");
    }


    @OnClick({R.id.tv_promotion_record, R.id.iv_back})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_promotion_record:
                startActivity(new Intent(this, PromotionRecordAc.class));
                break;
        }
    }


    private void getAgreement(String type) {
        ApiUtil.getHomeApi().getAgreement(type).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<AgreementBean>>() {
            @Override
            public void onSuccess(ApiResponse<AgreementBean> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            if (data.getData().getInfo().size() > 0) {
                                setContent(data.getData().getInfo().get(0).getPost_content());
                            }
                        }
                    }
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        }));
    }

    private void setContent(String post_content) {
        tv_rule.setText(Html.fromHtml(post_content, null, null));
    }
}
